<?php

/**
 * This is the model class for table "tbl_thanh_vien".
 *
 * The followings are the available columns in table 'tbl_thanh_vien':
 * @property string $id
 * @property string $ho_va_ten
 * @property string $cong_ty
 * @property string $so_cmt
 * @property string $so_dien_thoai
 * @property string $dia_chi
 * @property string $email
 * @property string $fax
 * @property string $role
 * @property string $ngay_sinh
 * @property string $ma_so_thue
 * @property string $ten_tai_khoan
 * @property string $mat_khau
 * @property string $id_nguoi_tao
 * @property string $id_nguoi_chinh_sua
 * @property string $ngay_tao
 * @property string $ngay_chinh_sua
 *
 * The followings are the available model relations:
 * @property FormDanhSachNhaThauThamGiaDauThau[] $formDanhSachNhaThauThamGiaDauThaus
 * @property FormKeHoachDauThau[] $formKeHoachDauThaus
 * @property FormKetQuaDauThau[] $formKetQuaDauThaus
 * @property FormThongBaoMoiThau[] $formThongBaoMoiThaus
 * @property FormThongTinKhac[] $formThongTinKhacs
 * @property TblLog[] $tblLogs
 */
class ThanhVien extends CActiveRecord
{
    const ACCOUNT_LOCKED = 0;

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'tbl_thanh_vien';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('ho_va_ten, cong_ty, email, fax, role, ten_tai_khoan, mat_khau', 'length', 'max' => 50),
            array('so_cmt, so_dien_thoai, id_nguoi_tao, id_nguoi_chinh_sua', 'length', 'max' => 20),
            array('dia_chi', 'length', 'max' => 250),
            array('ma_so_thue', 'length', 'max' => 100),
            array('ngay_sinh, ngay_tao, ngay_chinh_sua', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, ho_va_ten, cong_ty, so_cmt, so_dien_thoai, dia_chi, email, fax, role, ngay_sinh, ma_so_thue, ten_tai_khoan, mat_khau, id_nguoi_tao, id_nguoi_chinh_sua, ngay_tao, ngay_chinh_sua', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'formDanhSachNhaThauThamGiaDauThaus' => array(self::HAS_MANY, 'FormDanhSachNhaThauThamGiaDauThau', 'id_nguoi_tao'),
            'formKeHoachDauThaus' => array(self::HAS_MANY, 'FormKeHoachDauThau', 'id_nguoi_tao'),
            'formKetQuaDauThaus' => array(self::HAS_MANY, 'FormKetQuaDauThau', 'id_nguoi_tao'),
            'formThongBaoMoiThaus' => array(self::HAS_MANY, 'FormThongBaoMoiThau', 'id_nguoi_tao'),
            'formThongTinKhacs' => array(self::HAS_MANY, 'FormThongTinKhac', 'id_nguoi_tao'),
            'tblLogs' => array(self::HAS_MANY, 'TblLog', 'user_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'ho_va_ten' => 'Họ và tên',
            'cong_ty' => 'Công ty',
            'so_cmt' => 'Số CMT',
            'so_dien_thoai' => 'Số điện thoại',
            'dia_chi' => 'Địa chỉ',
            'email' => 'Email',
            'fax' => 'Fax',
            'role' => 'Role',
            'ngay_sinh' => 'Ngày tháng năm sinh',
            'ma_so_thue' => 'Mã số thuế',
            'ten_tai_khoan' => 'Tên tài khoản',
            'mat_khau' => 'Mật khẩu',
            'id_nguoi_tao' => 'Người tạo',
            'id_nguoi_chinh_sua' => 'Người chỉnh sửa',
            'ngay_tao' => 'Ngày tạo',
            'ngay_chinh_sua' => 'Ngày chỉnh sửa',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search()
    {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('ho_va_ten', $this->ho_va_ten, true);
        $criteria->compare('cong_ty', $this->cong_ty, true);
        $criteria->compare('so_cmt', $this->so_cmt, true);
        $criteria->compare('so_dien_thoai', $this->so_dien_thoai, true);
        $criteria->compare('dia_chi', $this->dia_chi, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('fax', $this->fax, true);
        $criteria->compare('role', $this->role, true);
        $criteria->compare('ngay_sinh', $this->ngay_sinh, true);
        $criteria->compare('ma_so_thue', $this->ma_so_thue, true);
        $criteria->compare('ten_tai_khoan', $this->ten_tai_khoan, true);
        $criteria->compare('mat_khau', $this->mat_khau, true);
        $criteria->compare('id_nguoi_tao', $this->id_nguoi_tao, true);
        $criteria->compare('id_nguoi_chinh_sua', $this->id_nguoi_chinh_sua, true);
        $criteria->compare('ngay_tao', $this->ngay_tao, true);
        $criteria->compare('ngay_chinh_sua', $this->ngay_chinh_sua, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return ThanhVien the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    public function beforeSave()
    {
        $pass = md5($this->mat_khau);
        $this->mat_khau = $pass;
        $this->ngay_tao = date("Y-m-d H:i:s");
        $this->role = 'member';
        return true;
    }
}
